package com.qjc.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qjc.demo.utils.FileUtils;
import com.qjc.demo.utils.HttpClientHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {

    @RequestMapping(value = "/request/test", method = RequestMethod.POST)
    public void applyRiskReportDetail(@RequestParam(value = "file1", required = false) MultipartFile file1,
                                      @RequestParam(value = "file2", required = false) MultipartFile file2,
                                      @RequestParam(value = "arg1", required = false) String arg1,
                                      @RequestParam(value = "arg2", required = false) String arg2) throws Exception {

        String URL = "http://IP:PORT/test";
        Map<String, String> headerParams = new HashMap<>();
        headerParams.put("token", "testToken");

        Map<String, MultipartFile> fileParams = new HashMap<>();
        //本地文件转为MultipartFile
//        String picPath = "/var/111.png";
//        MultipartFile file = FileUtils.pathToMultipartFile(picPath);
        //url转为MultipartFile
//        MultipartFile fileItem = FileUtils.urlToMutipartFile("https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=0.jpg", "asdfasdf.png");
        fileParams.put("file2", file2);
        fileParams.put("file1", file1);

        Map<String, String> otherParams = new HashMap<>();
        otherParams.put("arg1", arg1);
        otherParams.put("arg2", arg2);

        String s = HttpClientHelper.getInstance().uploadFile(URL, fileParams, otherParams, headerParams);
        System.err.println(s);
    }

    @RequestMapping(value = "/json/http", method = RequestMethod.POST)
    public void sendJsonHttpPost() {
        String URL = "http://IP:PORT/json/post/http";
        JSONObject jsonObject = new JSONObject();

        String s = HttpClientHelper.getInstance().sendJsonHttpPost(URL, JSON.toJSONString(jsonObject));
        System.err.println(s);
    }

}